home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / bsd_io.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  8KB  |  393 lines

  1. /* OS- and machine-dependent stuff for 4.[23] BSD UNIX */
  2.  
  3. /*
  4.     FILE: UNIX.io.c
  5.     
  6.     Routines:
  7.         ioinit()
  8.         iostop()
  9.         asy_init()
  10.         asy_stop()
  11.         asy_speed()
  12.         asy_output()
  13.         asy_recv()
  14.         dir()
  15.     Written or converted by Mikel Matthews, N9DVG
  16.     
  17.     If you want to use the select code, define SELECT in the makefile or
  18.     in this file.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sgtty.h>
  24. #include <signal.h>
  25. #include <sys/file.h>
  26. #include <sys/dir.h>
  27. #include <sys/time.h>
  28. #include <memory.h>
  29. #include <string.h>
  30. #include "global.h"
  31. #include "asy.h"
  32. #include "mbuf.h"
  33. #include "internet.h"
  34. #include "iface.h"
  35. #include "unix.h"
  36. #include "cmdparse.h"
  37.  
  38. #ifndef    B19200
  39. #define    B19200    EXTA
  40. #endif
  41.  
  42. #ifndef    B38400
  43. #define    B38400    EXTB
  44. #endif
  45.  
  46. struct asy asy[ASY_MAX];
  47. struct interface *ifaces;
  48. struct sgttyb mysavetty, savecon;
  49. int    IORser[ASY_MAX];
  50.  
  51.  
  52. /* Called at startup time to set up console I/O, memory heap */
  53. ioinit()
  54. {
  55.     struct sgttyb ttybuf;
  56.     extern void ioexit();
  57.  
  58.     (void) signal(SIGHUP, ioexit);
  59.     (void) signal(SIGINT, ioexit);
  60.     (void) signal(SIGQUIT, ioexit);
  61.     (void) signal(SIGTERM, ioexit);
  62.  
  63.     ioctl(0,TIOCGETP,&ttybuf);
  64.     savecon = ttybuf;
  65.     ttybuf.sg_flags &= ~ECHO;
  66.     ttybuf.sg_flags |= CBREAK;
  67.     ioctl(0,TIOCSETP,&ttybuf);
  68. }
  69. /* Called just before exiting to restore console state */
  70. void
  71. iostop()
  72. {
  73.     setbuf(stdout,NULLCHAR);
  74.  
  75.     while(ifaces != NULLIF){
  76.         if(ifaces->stop != NULLFP)
  77.             (*ifaces->stop)(ifaces->dev);
  78.         ifaces = ifaces->next;
  79.     }
  80.  
  81.     ioctl(0,TIOCSETP,&savecon);
  82. }
  83.  
  84. void
  85. ioexit()
  86. {
  87.     iostop();
  88.     exit(0);
  89. }
  90.  
  91. /* Initialize asynch port "dev" */
  92. /*ARGSUSED*/
  93. int slipisopen;
  94. /*ARGSUSED*/
  95. int
  96. asy_init(dev,arg1,arg2,bufsize)
  97. int16 dev;
  98. char *arg1,*arg2;
  99. unsigned bufsize;
  100. {
  101.     register struct asy *ap;
  102.     extern struct interface *ifaces;
  103.     struct sgttyb   sgttyb;
  104.  
  105.     ap = &asy[dev];
  106.  
  107.     if (ap == NULL || dev >= nasy)
  108.         return(-1);
  109.  
  110.     ap->tty = malloc((unsigned)(strlen(arg2)+1));
  111.     strcpy(ap->tty, arg2);
  112.  
  113.     printf("asy_init: tty name = %s\n", ap->tty);
  114.     
  115.  
  116.     if ((IORser[dev] = open (ap->tty, (O_RDWR), 0)) < 0)
  117.     {
  118.         perror ("Could not open device IORser");
  119.         return (-1);
  120.     }
  121.  /* 
  122.   * get the stty structure and save it 
  123.   */
  124.  
  125.     if (ioctl (IORser[dev], TIOCGETP, &mysavetty) < 0)
  126.     {
  127.         perror ("ioctl failed on device");
  128.         return (-1);
  129.     }
  130.  /* 
  131.   * copy over the structure 
  132.   */
  133.  
  134.     sgttyb = mysavetty;
  135.     sgttyb.sg_flags = (RAW | ANYP | CRMOD);
  136.     sgttyb.sg_ispeed = sgttyb.sg_ospeed = B9600;
  137.  
  138.  /* 
  139.   * now set the modes and flags 
  140.   */
  141.     if (ioctl (IORser[dev], TIOCSETP, &sgttyb) < 0)
  142.     {
  143.         perror ("ioctl could not set parameters for IORser");
  144.         return (-1);
  145.     }
  146.  
  147.     return 0;
  148. }
  149.  
  150. /*ARGSUSED*/
  151. int
  152. asy_stop(iface)
  153. struct interface *iface;
  154. {
  155. }
  156.  
  157. /* Set asynch line speed */
  158. int
  159. asy_speed(dev,speed)
  160. int16 dev;
  161. int speed;
  162. {
  163.     struct sgttyb sgttyb;
  164.  
  165.     if(speed == 0 || dev >= nasy)
  166.         return(-1);
  167.     asy[dev].speed = speed;
  168.  
  169.     if (ioctl (IORser[dev], TIOCGETP, &sgttyb) < 0)
  170.     {
  171.         perror ("ioctl could not get parameters");
  172.         return (-1);
  173.     }
  174.     switch(speed)
  175.     {
  176.         case 0:
  177.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B0;
  178.             break;
  179.         case 50:
  180.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B50;
  181.             break;
  182.         case 75:
  183.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B75;
  184.             break;
  185.         case 110:
  186.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B110;
  187.             break;
  188.         case 134:
  189.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B134;
  190.             break;
  191.         case 150:
  192.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B150;
  193.             break;
  194.         case 200:
  195.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B200;
  196.             break;
  197.         case 300:
  198.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B300;
  199.             break;
  200.         case 600:
  201.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B600;
  202.             break;
  203.         case 1200:
  204.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B1200;
  205.             break;
  206.         case 1800:
  207.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B1800;
  208.             break;
  209.         case 2400:
  210.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B2400;
  211.             break;
  212.         case 4800:
  213.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B4800;
  214.             break;
  215.         case 9600:
  216.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B9600;
  217.             break;
  218.         case 19200:
  219.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B19200;
  220.             break;
  221.         case 38400:
  222.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B38400;
  223.             break;
  224.         default:
  225.             printf("asy_speed: Unknown speed (%d)\n", speed);
  226.             break;
  227.     }
  228.     if (ioctl (IORser[dev], TIOCSETP, &sgttyb) < 0) {
  229.         perror ("ioctl could not set parameters for IORser");
  230.         return (-1);
  231.     }
  232.     return(0);
  233. }
  234. /* Send a buffer to serial transmitter */
  235. asy_output(dev,buf,cnt)
  236. unsigned dev;
  237. char *buf;
  238. unsigned short cnt;
  239. {
  240.     if(dev >= nasy)
  241.         return(-1);
  242.     if ( write(IORser[dev], buf, (int)cnt) < cnt)
  243.     {
  244.         perror("asy_output");
  245.         printf("asy_output: error in writing to device %d\n", dev);
  246.         return(-1);
  247.     }
  248.     return(0);
  249. }
  250. /* Receive characters from asynch line
  251.  * Returns count of characters read
  252.  */
  253. unsigned
  254. asy_recv(dev,buf,cnt)
  255. int dev;
  256. char *buf;
  257. unsigned cnt;
  258. {
  259. #define IOBUFLEN    256
  260.     unsigned tot;
  261.     long amount;
  262.     int r;
  263.     static struct {
  264.         char buf[IOBUFLEN];
  265.         char *data;
  266.         int cnt;
  267.     } IOBUF[ASY_MAX];
  268.  
  269. #ifdef SELECT
  270.     int    mask;
  271.     int    writemask;
  272.     int    ok;
  273.     struct timeval timeout;
  274.     timeout.tv_sec = 0;
  275.     timeout.tv_usec = 35;
  276.     mask = (1<<IORser[dev]);
  277.     writemask = (1<<IORser[dev]);
  278.     ok = 0;
  279.     tot = 0;
  280.     ok = select(mask, &mask, 0, 0, &timeout);
  281.     if ( mask & (1<<IORser[dev]))
  282.     {
  283.         tot = read(IORser[dev], buf, cnt);
  284.     }
  285.     return (tot);
  286. #else
  287.     tot = 0;
  288.     /* fill the read ahead buffer */
  289.         if(IOBUF[dev].cnt == 0) {
  290.                 IOBUF[dev].data = IOBUF[dev].buf;
  291.         amount = 0;
  292.         if ((r = ioctl( IORser[dev], FIONREAD, &amount)) != -1) {
  293.             if ( amount > 0)
  294.                 r = read(IORser[dev], buf, (int)cnt);
  295.             else
  296.                 r = 0;
  297.         }
  298.                 r = read(IORser[dev], IOBUF[dev].data, IOBUFLEN);
  299.                 /* check the read */
  300.                 if (r == -1) {
  301.                         IOBUF[dev].cnt = 0;     /* bad read */
  302.                         perror("asy_recv");
  303.                         printf("asy_recv: error in reading from device %d\n", dev);
  304.                         return(0);
  305.                 } else
  306.                         IOBUF[dev].cnt = r;
  307.         }
  308.         r = 0;  /* return count */
  309.         /* fetch what you need with no system call overhead */
  310.         if(IOBUF[dev].cnt > 0) {
  311.                 if(cnt == 1) { /* single byte copy, do it here */
  312.                         *buf = *IOBUF[dev].data++;
  313.                         IOBUF[dev].cnt--;
  314.                         r = 1;
  315.                 } else { /* multi-byte copy, left memcpy do the work */
  316.                         unsigned n = min(cnt, IOBUF[dev].cnt);
  317.                         memcpy(buf, IOBUF[dev].data, n);
  318.                         IOBUF[dev].cnt -= n;
  319.                         IOBUF[dev].data += n;
  320.                         r = n;
  321.                 }
  322.         }
  323.         tot = (unsigned int) r;
  324.     return (tot);
  325. #endif SELECT
  326. }
  327.  
  328. /* Generate a directory listing by opening a pipe to /bin/ls.
  329.  * If full == 1, give a full listing; else return just a list of names.
  330.  */
  331. FILE *
  332. dir(path,full)
  333. char *path;
  334. int full;
  335. {
  336.         FILE *fp;
  337.         char cmd[1024];
  338.  
  339.         if (path == NULLCHAR || path[0] == '\0')
  340.                 path = ".";
  341.  
  342.         if (full)
  343.                 sprintf(cmd,"ls -l %s", path);
  344.         else
  345.                 sprintf(cmd, "ls %s", path);
  346.  
  347.         if ((fp = popen(cmd,"r")) == NULLFILE) {
  348.                 perror("popen");
  349.                 return NULLFILE;
  350.         }
  351.  
  352.         return fp;
  353. }
  354.  
  355. asy_ioctl(interface, argc, argv)
  356. struct interface *interface;
  357. int    argc;
  358. char    *argv[];
  359. {
  360.     if (argc < 1) {
  361.         printf("%d\r\n", asy[interface->dev].speed);
  362.         return 0;
  363.     }
  364.     return asy_speed(interface->dev, atoi(argv[0]));
  365. }
  366.  
  367. int
  368. OpenPty()
  369. {
  370.         extern int      errno;
  371.         int             pty;
  372.         int             letcnt=0, numcnt=0;
  373.  
  374.         static char     *letters = "pqrs",
  375.                         *numbers = "0123456789abcdef";
  376.         static char master[] = "/dev/ptyXX";
  377.         static int      letmax, nummax;
  378.  
  379.         letmax=strlen(letters)-1, nummax=strlen(numbers)-1;
  380.         do {
  381.                 master[strlen("/dev/pty")] = letters[letcnt];
  382.                 master[strlen("/dev/ptyX")] = numbers[numcnt];
  383.                 if (letcnt > letmax) {
  384.                         return -1;
  385.                 } else if (++numcnt > nummax) {
  386.                         letcnt++;
  387.                         numcnt = 0;
  388.                 }
  389.         } while ((pty=open(master, O_RDWR)) < 0);
  390.  
  391.         return(pty);
  392. }
  393.